#include "MCArc2D.h"
#include <MCVector2.h>
#include <iostream>
#include <MCMathUtils.h>

MCArc2D::MCArc2D() :
    _center(MCPoint2D()),
    _radius(0),
    _startAngle(0),
    _endAngle(0)
{
}

MCArc2D::MCArc2D(MCPoint2D center, double radius, double startAngle, double endAngle) :
    _center(center),
    _radius(radius),
    _startAngle(startAngle),
    _endAngle(endAngle)
{
}

MCPlaneItemType MCArc2D::type()
{
    return MCPlaneItemType::ARC;
}

void MCArc2D::setPositionAt(const MCPoint2D &at)
{
    _center.setPositionAt(at);
}

void MCArc2D::move(const MCVector2 &to)
{
    _center.move(to);
}

void MCArc2D::scale(double scale)
{
}

void MCArc2D::transform(const MCMatrix3 &transform)
{
}

MCPoint2D MCArc2D::center() const
{
    return _center;
}

MCPoint2D MCArc2D::startPoint() const
{
    MCVector2 vec(_radius, 0);
    double vx = vec.x;
    double vy = vec.y;
    double x = vx * std::cos(_startAngle) - vy * std::sin(_startAngle);
    double y = vx * std::sin(_startAngle) + vy * std::cos(_startAngle);

    return _center + MCVector2(x, y);
}

MCPoint2D MCArc2D::endPoint() const
{
    MCVector2 vec(_radius, 0);
    double vx = vec.x;
    double vy = vec.y;
    double x = vx * std::cos(_endAngle) - vy * std::sin(_endAngle);
    double y = vx * std::sin(_endAngle) + vy * std::cos(_endAngle);

    return _center + MCVector2(x, y);
}

MCPoint2D MCArc2D::midPoint() const
{
    double angle = (_startAngle + _endAngle) / 2;

    if (_endAngle < _startAngle)
    {
        angle = (_startAngle + _endAngle + M_PI * 2) / 2;
    }

    MCVector2 vec(_radius, 0);
    double vx = vec.x;
    double vy = vec.y;
    double x = vx * std::cos(angle) - vy * std::sin(angle);
    double y = vx * std::sin(angle) + vy * std::cos(angle);

    return _center + MCVector2(x, y);
}

double MCArc2D::radius() const
{
    return _radius;
}

double MCArc2D::startAngle() const
{
    return _startAngle;
}

double MCArc2D::endAngle() const
{
    return _endAngle;
}
